//加载页面上时 执行里面的方法，。
$(function () {
    selectContactUs();
})

function selectContactUs() {
    let data1 = {
        pageSize: $("#pageSize").val(),
        pageNumber: $("#pageNumber").val(),
    }
    let data = myAjax("/back/contactUs/findContactUs", data1, "get");
    let list = data.list;
    let html = '';
    $("#count").html(data.count);
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let a = Math.ceil(count / pageSize);
    $("#pageCount").html(a);
    //页数 改变 上一页下一页颜色=================================================================
    let pageNumber = parseInt($("#pageNumber").val());
    if (pageNumber <= 1) {
        $("#lastPage").attr('disabled', "true");
        $("#lastPage").css("background-color", "silver");
        $("#nextPage").removeAttr("disabled");
        $("#nextPage").removeAttr('style', "");

    } else if (pageNumber == a) {
        $("#nextPage").attr('disabled', "true");
        $("#lastPage").removeAttr("disabled");
        $("#nextPage").css("background-color", "silver");
        $("#lastPage").removeAttr('style', "");
    }

    html += '<tr>\n' +
        '            <td>' + list.id + '</td>\n' +
        '            <td>' + list.location + '</td>\n' +
        '            <td>' + list.contactphone + '</td>\n' +
        '            <td>' + list.orderphone + '</td>\n' +
        '            <td>' + list.email + '</td>\n' +
        '            <td>' + list.net + '</td>\n' +
        '            <td>' + list.line + '</td>\n' +
        '            <td>' + list.wechat + '</td>\n' +
        '            <td>' + list.twitter + '</td>\n' +
        '            <td><a href="javascript:edit(' + list.id + ')">编辑</a>&nbsp;' +
        '            </td>\n' +
        '        </tr>';
    $("#findContactUs").html(html);

}


//通过id编辑===============================================
function edit(id) {
    localStorage.setItem("contactUsId", id);
    $("#right").load("/back/html/contactUs/contactUsUpdate.html");
}

//通过id删除==============================================
function del(id) {
    let data = myAjax("/back/contactUs/deleteContactUsById", {id: id}, "post");
    if (data.result == 1) {
        alert("删除成功");
        $('#right').load('/back/html/contactUs/contactUsManage.html');
    } else if (data.result == 0) {
        alert("删除失败");
        $('#right').load('/back/html/contactUs/contactUsManage.html');
    }
}

//模糊查询一些数据
function findContactUsSome() {
    let data1 = {
        contactUsname: $("#ContactUsname").val(),
        phone: $("#phone").val(),
        sex: $("#sex").val(),
        firstbirthday: $("#firstbirthday").val(),
        lastbirthday: $("#lastbirthday").val(),
        hobby: hb()
    }
    let data = myAjax("/findSome", data1, 'get');
    html += '<tr>\n' +
        '            <td>' + data[i].id + '</td>\n' +
        '            <td>' + data[i].name + '</td>\n' +
        '            <td>' + data[i].email + '</td>\n' +
        '            <td>' + data[i].content + '</td>\n' +
        '            <td><a href="javascript:edit(' + data[i].id + ')">编辑</a>&nbsp;' +
        '            <a href="javascript:del(' + data[i].id + ')">删除</a>' +
        '        </td>\n' +
        '        </tr>';

    $("#findContactUs").html(html);

}

function hb() {
    let hb = [];
    $("input:checkbox[name='hobby']:checked").each(function () {
        let state = ($(this).prop('checked'));
        if (state) {
            hb.push($(this).val());
        }
    });
    hb = hb.join(',');
    return hb;

}

function firstPage() {
    $("#pageNumber").val(1);
    $("#nextPage").removeAttr('style', "");
    selectContactUs();
}

function lastPage() {
    let a = parseInt($("#pageNumber").val());
    if (a > 1) {
        $("#pageNumber").val(($("#pageNumber").val() - 1));
        $("#nextPage").removeAttr('style', "");
    }
    selectContactUs();
}

function nextPage() {
    let a = parseInt($("#pageNumber").val());
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let b = Math.ceil(count / pageSize);
    if (a < b) {
        $("#lastPage").removeAttr('style', "");
        $("#pageNumber").val(a + 1);
    }
    selectContactUs();
}

function changes() {
    let a = parseInt($("#pageNumber").val());
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let b = Math.ceil(count / pageSize);
    if (a <= 1) {
        $("#pageNumber").val(1);
        $("#lastPage").attr('disabled', "true");
    } else if (a >= b) {
        $("#pageNumber").val(b);
        $("#nextPage").attr('disabled', "true");
    } else {
        $("#lastPage").removeAttr("disabled");
    }
}

function endPage() {
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let a = Math.ceil(count / pageSize);
    $("#pageNumber").val(a);
    $("#lastPage").removeAttr('style', "");
    selectContactUs();
}